﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.ComponentModel;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    public partial class TVKADR
    {
        public TVKADR()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VVKADR();
            this.DataContext = MV;
            InitializeComponent();
        }

        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
        }
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            base.Window_Loaded(sender, e);
        }

        public override void QueryCommand(object sender, RoutedEventArgs e)
        {
            ORDERNUM = "";
            ORDERNUM = KRS + LFD + POS + KUNDNR + "K";
            base.QueryCommand(sender, e);
        }

        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            ((VVKADR)MV).UpdateVKADR(dwMaster, app.company, 0);
            base.SaveCommand(sender, e);
        }

        public override void EditCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
            {
                MessageBox.Show("请先执行查询");
                return;
            }
            base.EditCommand(sender, e);
        }

        public override void LOADBYAUF()
        {
            if (ORDERNUM.Length < 19)
                return;
            TBMain.Clear();
            KRS = ORDERNUM.Substring(1, 2);
            LFD = ORDERNUM.Substring(3, 6);
            POS = ORDERNUM.Substring(9, 3);
            if (!IsLoaded)
                return;
            dwData = TBMain.Clone().NewRow();
            dwData["VKAMANDANT"] = app.Mandant;
            dwData["VKASA"] = "  ";
            dwData["VKAAUFKRS"] = KRS;
            dwData["VKAAUFLFD"] = LFD;
            dwData["VKAAUFPOS"] = POS;
            SResult = ((VVKADR)MV).QueryVKADR(dwData, 1);
            MergeData(SResult, true, true);
            base.LOADBYAUF();
        }

        private void VKADRCommand(object sender, RoutedEventArgs e)
        {
            if (!IsEdit)
                return;
            if (dwMaster == null)
                return;
            if (dwKDR == null)
            {
                MessageBox.Show("请选择一个客户地址");
                return;
            }
            TMV.VKABNAME1 = dwKDR["KDANAME1"].ToString();
            TMV.VKABNAME2 = dwKDR["KDANAME2"].ToString();
            TMV.VKABSTRASSE1 = dwKDR["KDASTRASSE1"].ToString();
            TMV.VKABSTRASSE2 = dwKDR["KDASTRASSE2"].ToString();
            TMV.VKABLKZ = dwKDR["KDALAND"].ToString();
            TMV.VKABPLZ = dwKDR["KDAPLZ"].ToString(); 
            TMV.VKABORT = Helper.Left((dwKDR["KDATTEX"].ToString().Trim() + ":" + dwKDR["KDATEL"].ToString().Trim()).PadRight(40), 40);
            TMV.VKABPOSTF = Helper.Left(dwKDR["KDAPOSTFACH"].ToString().PadRight(15), 15);
            TMV.VKAANZ2 = "01";
            TMV.VKAADRSLK12 = dwKDR["KDAADRSL"].ToString();
            TMV.VKAKUNDNRK12 = dwKDR["KDAKUNDNR"].ToString();
            TMV.VKAANZK12 = "01";
            if (dwMaster == null)
                return;
        }

        private void VVADRCommand(object sender, RoutedEventArgs e)
        {
            if (!IsEdit)
                return;
            if (dwMaster == null)
                return;
            if (dwKDR == null)
                return;
            TMV.VKALNAME1 = dwKDR["KDANAME1"].ToString();
            TMV.VKALNAME2 = dwKDR["KDANAME2"].ToString();
            TMV.VKALSTRASSE1 = dwKDR["KDASTRASSE1"].ToString();
            TMV.VKALSTRASSE2 = dwKDR["KDASTRASSE2"].ToString();
            TMV.VKALLKZ = dwKDR["KDALAND"].ToString();
            TMV.VKALPLZ = dwKDR["KDAPLZ"].ToString();
            TMV.VKALORT =Helper.Left( (dwKDR["KDATTEX"].ToString().Trim()+":"+dwKDR["KDATEL"].ToString().Trim()).PadRight(40),40); 
            TMV.VKALBZBEZ = Helper.Left(dwKDR["KDAPOSTFACH"].ToString().PadRight(13), 13);
            TMV.VKAKUNDNR1 = dwKDR["KDAKUNDNR"].ToString();
            TMV.VKAANZ1 = "01".ToString();
            TMV.VKAADRSLK11 = dwKDR["KDAADRSL"].ToString();
            TMV.VKAKUNDNRK11 = dwKDR["KDAKUNDNR"].ToString();
            TMV.VKAANZK11 = "01".ToString();
            if (dwMain == null)
                return;

        }

        private void VRADRCommand(object sender, RoutedEventArgs e)
        {
            if (!IsEdit)
                return;
            if (dwMaster == null)
                return;
            if (dwKDR == null)
                return;
            TMV.VKAADRSL3 = dwKDR["KDAADRSL"].ToString();
            TMV.VKAKUNDNR3 = dwKDR["KDAKUNDNR"].ToString();
            TMV.VKAANZK22 = "00";
            TMV.VKAANZK13 = "00";
            if (dwMaster == null)
                return;
        }

        public override void OnVVTChg(string vvt)
        {
            ORDERNUM = "";
            ORDERNUM = vvt;
            base.OnVVTChg(vvt);
        }
        public override void OnVKPChg(string value)
        {
            ORDERNUM = "";
            ORDERNUM = value;
            base.OnVKPChg(value);
        }

    }
}
